package com.neutech.dao.impl;

import com.neutech.dao.FoodDAO;
import com.neutech.entity.business;
import com.neutech.entity.food;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class foodAchieve implements FoodDAO {

    Connection connection = databaseConnection.getConnection();

    static PreparedStatement ps = null;
    static ResultSet rs = null;
    static food food = new food();

    @Override
    public List<food> listFood() {
        List<food> list = new ArrayList<>();
        try{
            ps = connection.prepareStatement("select * from food");
            rs = ps.executeQuery();
            while (rs.next()){
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString(("foodName")));
                food.setFoodExplain(rs.getString(("businessAddress")));
                food.setFoodPrice(rs.getString(("businessAddress")));
                food.setBusinessId(rs.getString(("businessId")));
                System.out.println(food);
                list.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int addFood(food food) {
        int count = 0;
        try{
            ps = connection.prepareStatement("insert into food(foodId,foodName,foodExplain,foodPrice,businessId) values(?,?,?,?,?)");
            ps.setInt(1,food.getFoodId());
            ps.setString(2,food.getFoodName());
            ps.setString(3,food.getFoodExplain());
            ps.setString(4,food.getFoodPrice());
            ps.setString(5,food.getBusinessId());

            ps.executeUpdate();
            count++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int updatefood(food food) {
        int count = 0;
        try{
            ps = connection.prepareStatement("update food set foodName=?,foodExplain=?,foodPrice=?,businessId=? where foodId=?");
            ps.setString(1,food.getFoodName());
            ps.setString(2,food.getFoodExplain());
            ps.setString(3,food.getFoodPrice());
            ps.setString(4,food.getBusinessId());
            ps.setInt(5,food.getFoodId());
            ps.executeUpdate();
            count++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int deletefoof(String Name) {
       int count = 0;
       try{
           ps = connection.prepareStatement("delete from food where foodName=?");
           ps.setString(1,Name);
           ps.executeUpdate();
           count++;
       } catch (SQLException e) {
           e.printStackTrace();
       }
       return count;
    }
}
